cmake_minimum_required(VERSION 3.27)

# 定义项目名称
project(CNC_Simulation)

# 设置 C++ 标准为 C++17
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_WIN32_EXECUTABLE ON)

# 启用 Qt 的自动生成机制
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)

# 设置 Qt 安装路径
set(CMAKE_PREFIX_PATH "C:/Qt/6.5.3/mingw_64")

# 查找 Qt6 组件
find_package(Qt6 REQUIRED COMPONENTS Core Gui Widgets Charts OpenGL OpenGLWidgets)

# 设置源文件
set(Main_SOURCE
        src/main.cpp
        src/MainWindow/MainWindow.cpp
        src/ReadFileDialog/ReadFileDialog.cpp
        src/ToolParameterDialog/ToolParameterDialog.cpp
        src/FileListDialog/FileListDialog.cpp
        src/QGraphicsView/QGraphicsView.cpp
        src/ToolMover/ToolMover.cpp
        src/NCMain/NCMain.cpp
        src/QSS.h
)

# 创建可执行文件 CNC_Simulation
add_executable(CNC_Simulation ${Main_SOURCE})

# 指定 CNC_Simulation 可执行文件的输出路径
set_target_properties(CNC_Simulation PROPERTIES
        RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/../bin"
)

target_link_libraries(CNC_Simulationgit)


# 链接 Qt 库
target_link_libraries(CNC_Simulation NCMain
        Qt6::Core
        Qt6::Gui
        Qt6::Widgets
        Qt6::Charts
        Qt6::OpenGL
        Qt6::OpenGLWidgets
)

# 设置静态链接标准库
if (CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -static-libgcc -static-libstdc++")
endif()

# 静态链接所有库
set(CMAKE_EXE_LINKER_FLAGS "-static")

# 针对 Windows 平台的特定设置
if (WIN32)
    set(DEBUG_SUFFIX "")

    if (MSVC AND CMAKE_BUILD_TYPE MATCHES "Debug")
        set(DEBUG_SUFFIX "d")
    endif()

    # 设置 Qt 安装路径
    set(QT_INSTALL_PATH "${CMAKE_PREFIX_PATH}")

    # 确保 Qt 的 bin 目录存在
    if (NOT EXISTS "${QT_INSTALL_PATH}/bin")
        set(QT_INSTALL_PATH "${QT_INSTALL_PATH}/..")
    endif()

    # 复制 Qt 库到输出目录
    foreach(QT_LIB Core Gui Widgets Charts OpenGL OpenGLWidgets)
        add_custom_command(TARGET CNC_Simulation POST_BUILD
                COMMAND ${CMAKE_COMMAND} -E copy_if_different "${QT_INSTALL_PATH}/bin/Qt6${QT_LIB}${DEBUG_SUFFIX}.dll" "$<TARGET_FILE_DIR:CNC_Simulation>"
                COMMENT "Copying Qt6${QT_LIB}${DEBUG_SUFFIX}.dll to output directory"
        )
    endforeach()

    # 复制 MinGW 的所有库 DLL 到输出目录
    set(MINGW_LIB_PATH "C:/Qt/Tools/mingw1120_64/bin")
    file(GLOB STANDARD_LIB_FILES "${MINGW_LIB_PATH}/*.dll")

    foreach(STANDARD_LIB ${STANDARD_LIB_FILES})
        add_custom_command(TARGET CNC_Simulation POST_BUILD
                COMMAND ${CMAKE_COMMAND} -E copy_if_different "${STANDARD_LIB}" "$<TARGET_FILE_DIR:CNC_Simulation>"
                COMMENT "Copying ${STANDARD_LIB} to output directory"
        )
    endforeach()
endif()
